/*
 * LDIR
 *
 * LDIR, a GPS cartography application built for Let's Do It Romania eco
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 *
 * http://generasoftware.com/
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <avkon.hrh>
#include <avkon.rsg>
#include <eikmenup.h>
#include <aknappui.h>
#include <eikcmobs.h>
#include <barsread.h>
#include <stringloader.h>
#include <eikfpne.h>
#include <eikenv.h>
#include <aknpopupfieldtext.h>
#include <aknnumedwin.h>
#include <gdi.h>
#include <eikedwin.h>
#include <LDIR.rsg>
#include "LDIRWaste.h"
#include "WasteInfo.h"
#include "WasteCache.h"
#include "Settings.h"
#include "GPSUtils.h"
#include "LDIR.hrh"
#include "LDIRWaste.hrh"

/**
 * Construct the CLDIRWaste instance
 * @param aCommandObserver command observer
 */
CLDIRWaste::CLDIRWaste( CWasteInfo& aWasteInfo,
    MEikCommandObserver* aCommandObserver ) :
    iWasteInfo( aWasteInfo ), iCommandObserver( aCommandObserver )
    {
    }

/**
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aCommandObserver command observer
 * @return initialized instance of CLDIRWaste
 */
CLDIRWaste* CLDIRWaste::NewL( CWasteInfo& aWasteInfo,
    MEikCommandObserver* aCommandObserver )
    {
    CLDIRWaste* self = CLDIRWaste::NewLC( aWasteInfo, aCommandObserver );
    CleanupStack::Pop( self );
    return self;
    }

/**
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aCommandObserver command observer
 * @return new instance of CLDIRWaste
 */
CLDIRWaste* CLDIRWaste::NewLC( CWasteInfo& aWasteInfo,
    MEikCommandObserver* aCommandObserver )
    {
    CLDIRWaste* self = new ( ELeave ) CLDIRWaste( aWasteInfo, aCommandObserver );
    CleanupStack::PushL( self );
    self->ConstructL();
    return self;
    }

/**
 * Handle key event (override)
 * @param aKeyEvent key event
 * @param aType event code
 * @return EKeyWasConsumed if the event was handled, else EKeyWasNotConsumed
 */
TKeyResponse CLDIRWaste::OfferKeyEventL( const TKeyEvent& aKeyEvent,
    TEventCode aType )
    {
    //    if ( aType == EEventKey && ( aKeyEvent.iScanCode == EStdKeyDevice3 )
    //        && !IsEditable() )
    //        {
    //        ProcessCommandL( EAknFormCmdEdit );
    //        return EKeyWasConsumed;
    //        }

    return CAknForm::OfferKeyEventL( aKeyEvent, aType );
    }

/**
 * Destroy any instance variables
 */
CLDIRWaste::~CLDIRWaste()
    {
    }

/**
 * Called to handle "Save" menu item. Displays save query.
 * @return TBool ETrue if the form data is to be saved, EFalse otherwise
 */
TBool CLDIRWaste::QuerySaveChangesL()
    {
    TBool isAnsYes( CAknForm::QuerySaveChangesL() );
    return isAnsYes;
    }

/**
 * Called from QuerySaveChangesL when changes made to the form are to be saved.
 * @return TBool ETrue if the form data has been saved, EFalse otherwise
 */
TBool CLDIRWaste::SaveFormDataL()
    {
    iLength
        = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteLength ) );
    iLength->GetTextAsInteger( ( TInt& ) iWasteInfo.iLength );

    iWidth = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteWidth ) );
    iWidth->GetTextAsInteger( ( TInt& ) iWasteInfo.iWidth );

    iHeight
        = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteHeight ) );
    iHeight->GetTextAsInteger( ( TInt& ) iWasteInfo.iHeight );

    iDispersed = static_cast<CAknPopupFieldText*> ( ControlOrNull(
        ELDIRWasteDispersed ) );
    iWasteInfo.iIsDispersed = iDispersed->CurrentValueIndex();

    iBagEq = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteBagEq ) );
    iBagEq->GetTextAsInteger( ( TInt& ) iWasteInfo.iBagEq );

    iUnreciclable = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteUnreciclable ) );
    iUnreciclable->GetTextAsInteger( ( TInt& ) iWasteInfo.iUnreciclable );

    iGlass = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteGlass ) );
    iGlass->GetTextAsInteger( ( TInt& ) iWasteInfo.iGlass );

    iMetal = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteMetal ) );
    iMetal->GetTextAsInteger( ( TInt& ) iWasteInfo.iMetal );

    iPlastic = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWastePlastic ) );
    iPlastic->GetTextAsInteger( ( TInt& ) iWasteInfo.iPlastic );

    iCardboard = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteCardboard ) );
    iCardboard->GetTextAsInteger( ( TInt& ) iWasteInfo.iCardboard );

    iRubber
        = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteRubber ) );
    iRubber->GetTextAsInteger( ( TInt& ) iWasteInfo.iRubber );

    iOversized = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteOversized ) );
    iOversized->GetTextAsInteger( ( TInt& ) iWasteInfo.iOversized );

    iConstruction = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteConstruction ) );
    iConstruction->GetTextAsInteger( ( TInt& ) iWasteInfo.iOversized );

    iExcluded = static_cast<CAknPopupFieldText*> ( ControlOrNull(
        ELDIRWasteExcluded ) );
    iWasteInfo.iExcluded = iExcluded->CurrentValueIndex();

    iWasteInfo.iComment.Close();
#ifndef __TEST__
    iComment = static_cast<CEikEdwin*> ( ControlOrNull( ELDIRWasteComment ) );
        {

        iWasteInfo.iComment.CreateMaxL( 255 );
        iComment->Text()->Extract( iWasteInfo.iComment, 0,
            iWasteInfo.iComment.MaxLength() );
        }
#else
    iWasteInfo.iComment.CreateL( _L("test aplicatii mobile") );
#endif

    CSettings& settings = CSettings::InstanceL();
    settings.PendingUploadCache().AppendL( settings.WorkingWaste() );
    settings.SetWorkingWaste( NULL );
    settings.StoreSettingsL();

    return ETrue;
    }

/**
 * Called from QuerySaveChangesL when changes made to the form are discarded.
 */
void CLDIRWaste::DoNotSaveFormDataL()
    {
    CSettings& settings = CSettings::InstanceL();
    delete settings.WorkingWaste();
    settings.SetWorkingWaste( NULL );
    }

void CLDIRWaste::ProcessCommandL( TInt aCommandId )
    {
    CAknForm::ProcessCommandL( aCommandId );
    if ( aCommandId = EAknFormCmdSave )
        {
        iAvkonAppUi->ProcessCommandL( ECmdMain );
        }
    }

/**
 * Called from DoNotSaveFormDataL when changes are cancelled.
 * Called from PreLayoutDynInitL to load initial values if needed.
 */
void CLDIRWaste::LoadFromDataL()
    {
    TBuf<10> buff;
    iLength
        = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteLength ) );
    iLength->SetValueL( iWasteInfo.iLength );
    buff.Num( iWasteInfo.iLength );
    iLength->SetTextL( &buff );
    iWidth = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteWidth ) );
    iWidth->SetValueL( iWasteInfo.iWidth );
    buff.Num( iWasteInfo.iWidth );
    iWidth->SetTextL( &buff );
    iHeight
        = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteHeight ) );
    iHeight->SetValueL( iWasteInfo.iHeight );
    buff.Num( iWasteInfo.iHeight );
    iHeight->SetTextL( &buff );
    iDispersed = static_cast<CAknPopupFieldText*> ( ControlOrNull(
        ELDIRWasteDispersed ) );
    iDispersed->SetFont( iEikonEnv->NormalFont() );
    iDispersed->SetCurrentValueIndex( iWasteInfo.iIsDispersed ? 1 : 0 );
    iBagEq = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteBagEq ) );
    iBagEq->SetValueL( iWasteInfo.iBagEq );
    buff.Num( iWasteInfo.iBagEq );
    iBagEq->SetTextL( &buff );
    iUnreciclable = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteUnreciclable ) );
    iUnreciclable->SetValueL( iWasteInfo.iUnreciclable );
    buff.Num( iWasteInfo.iUnreciclable );
    iUnreciclable->SetTextL( &buff );
    iGlass = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteGlass ) );
    iGlass->SetValueL( iWasteInfo.iGlass );
    buff.Num( iWasteInfo.iGlass );
    iGlass->SetTextL( &buff );
    iMetal = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteMetal ) );
    iMetal->SetValueL( iWasteInfo.iMetal );
    buff.Num( iWasteInfo.iMetal );
    iMetal->SetTextL( &buff );
    iPlastic = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWastePlastic ) );
    iPlastic->SetValueL( iWasteInfo.iPlastic );
    buff.Num( iWasteInfo.iPlastic );
    iPlastic->SetTextL( &buff );
    iCardboard = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteCardboard ) );
    iCardboard->SetValueL( iWasteInfo.iCardboard );
    buff.Num( iWasteInfo.iCardboard );
    iCardboard->SetTextL( &buff );
    iRubber
        = static_cast<CAknIntegerEdwin*> ( ControlOrNull( ELDIRWasteRubber ) );
    iRubber->SetValueL( iWasteInfo.iRubber );
    buff.Num( iWasteInfo.iRubber );
    iRubber->SetTextL( &buff );
    iOversized = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteOversized ) );
    iOversized->SetValueL( iWasteInfo.iOversized );
    buff.Num( iWasteInfo.iOversized );
    iOversized->SetTextL( &buff );
    iConstruction = static_cast<CAknIntegerEdwin*> ( ControlOrNull(
        ELDIRWasteConstruction ) );
    iConstruction->SetValueL( iWasteInfo.iConstruction );
    buff.Num( iWasteInfo.iConstruction );
    iConstruction->SetTextL( &buff );
    iExcluded = static_cast<CAknPopupFieldText*> ( ControlOrNull(
        ELDIRWasteExcluded ) );
    iExcluded->SetFont( iEikonEnv->NormalFont() );
    iExcluded->SetCurrentValueIndex( iWasteInfo.iExcluded ? 1 : 0 );
    iComment = static_cast<CEikEdwin*> ( ControlOrNull( ELDIRWasteComment ) );
        {
        iComment->SetTextL( &iWasteInfo.iComment );
        }
    }

/**
 * Initialize controls and settings before a Form is laid out.  (override)
 */
void CLDIRWaste::PreLayoutDynInitL()
    {
    LoadFromDataL();
    }

/**
 * Called when built-in menu is displayed.  (override)
 * CAknForm has a built-in menu. In view mode, the only menu item is "Edit" that switches the form to edit mode.
 * In edit mode, the items are "Add field" "Save" "Edit label" and "Delete field"
 * The code below removes all edit mode items except for "Save"
 * To use these items, remove the appropriate lines and override the appropriate CAknForm methods.
 * (E.g., to handle "Add field" remove the line for EAknFormCmdAdd and override CAknForm::AddItemL())
 */
void CLDIRWaste::DynInitMenuPaneL( TInt aResourceId, CEikMenuPane* aMenuPane )
    {
    CAknForm::DynInitMenuPaneL( aResourceId, aMenuPane );

    if ( aResourceId == R_AVKON_FORM_MENUPANE )
        {
        aMenuPane->SetItemDimmed( EAknFormCmdAdd, ETrue );
        aMenuPane->SetItemDimmed( EAknFormCmdLabel, ETrue );
        aMenuPane->SetItemDimmed( EAknFormCmdDelete, ETrue );

        /*
         // sample code to add a custom item with command id 'myCommandId'
         TInt pos;
         if ( !aMenuPane->MenuItemExists( myCommandId, pos ) )
         {
         CEikMenuPaneItem::SData menuItem;
         menuItem.iCommandId = myCommandId;
         menuItem.iCascadeId = 0;    // set for submenus
         menuItem.iFlags = 0;        // e.g. EEikMenuItemDimmed
         menuItem.iText = KMyCommandIdText;
         menuItem.iExtraText = _L("");
         aMenuPane->AddMenuItemL( menuItem );
         }
         */
        }
    }

/**
 * Handle a button press and tell whether it closes the dialog.  (override)
 * @param aButtonId the identifier for the button (avkon.hrh)
 */
TBool CLDIRWaste::OkToExitL( TInt aButtonId )
    {
    // The 'exit application on close' property automatically adds code to exit the
    // application when the right soft key is pressed. If this property is set to false,
    // and AddToStackL has been called on the Form object (e.g., it was not shown as a dialog),
    // code must be added manually to avoid a panic when exiting the application.
    if ( aButtonId == EAknSoftkeyBack )
        {
        if ( CAknForm::OkToExitL( EAknSoftkeyBack ) )
            {
            DoNotSaveFormDataL();
            iAvkonAppUi->ProcessCommandL( ECmdMain );
            return EFalse;
            }
        else
            {
            return EFalse;
            }
        }
    return CAknForm::OkToExitL( aButtonId );
    }

